﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace KABS_Commander
{
    public partial class KABS_Commander : Form
    {
        private bool needSelected = false;

        private string getStringCommand(string raw_cmd)
        {
            int skipNum = 12 * 2; // for 80 has two chars
            StringBuilder sb = new StringBuilder();
            raw_cmd = raw_cmd.Replace("\\x", "");
            if (raw_cmd.Length <= skipNum) return sb.ToString();
            for (int i = skipNum; i < raw_cmd.Length - 1; i += 2)
            {
                string hex_str = raw_cmd.Substring(i, 2);
                char c = ' ';
                try
                {
                    c = System.Convert.ToChar(System.Convert.ToInt32(hex_str, 16));
                    if (c == '\0' || c == '*')
                        break;
                }
                catch (Exception e)
                {
                    break;
                }
                sb.Append(c);
            }
            return sb.ToString();
        }

        private string getNewCommand(List<string> cmd_list)
        {
            string cmd = "";
            string param = "";
            // build cmd
            for (int i = 0; i < cmd_list.Count; i++)
            {
                cmd += cmd_list[i];
                cmd += "_";
                if (cmd_list[i] == "ACT") // anything after ACT is not a part of command
                    break;
            }
            // remove last "_"
            cmd = cmd.Remove(cmd.Length - 1);
            // build params
            for (int i = 0; i < cmd_list.Count; i++)
            {
                if (i % 2 == 1) // item 2, 4, 6....
                    param += (" " + cmd_list[i]);
            }
            // add params
            cmd += param;
            //if (cmd_list.First<string>() == "GPIO")
            //    cmd = "R_" + cmd;

            return cmd;
        }

        private string convertCmd(string s)
        {
            List<string> command_items = new List<string>();
            Match m = Regex.Match(s, @"<\w*>");

            while (m.Success)
            {
                command_items.Add(m.Value.Substring(1, m.Value.Length - 2));
                m = m.NextMatch();
            }

            if (command_items.Count == 0)
            {
                label_copyhint.Text = "Invalid Command String!";
                return null;
            }

            string final_command = getNewCommand(command_items);
            return final_command;
        }

        private void button_cmdCvt_Click(object sender, EventArgs e)
        {
            string raw_command = richTextBox_oldCommand.Text;
            string str_command = getStringCommand(raw_command);

            if (str_command.Length == 0)
            {
                label_copyhint.Text = "Invalid Command String!";
                return;
            }

            string final_command = convertCmd(str_command);

            Clipboard.SetText(final_command);
            label_copyhint.Text = "New command is in the clipboard.";
        }

        private void richTextBox_oldCommand_Leave(object sender, EventArgs e)
        {
            label_copyhint.Text = "Input command with prefix '\\x'.";
        }
    }
}